package com.benchmark.springboot.controller;

import com.power.common.model.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * 模拟延迟处理
 *
 * @author yu 2018/9/3.
 */
@Slf4j
@RestController
public class LongProcessController {

    /**
     * 延迟测试
     * @apiNote 使用睡眠模拟延迟，可用于测试延迟模拟测试，
     * 也可以用作测试Graceful down，测试graceful down时请使用kill pid
     * @param delay 延迟时间(ms)|1000
     * @return
     * @throws InterruptedException
     */
    @GetMapping("/long-process/{delay}")
    public CommonResult<String> pause(@PathVariable long delay) throws InterruptedException {
        Thread.sleep(delay);
        return CommonResult.ok().setResult("Process finished after sleep " + delay + "ms");
    }

    /**
     * 延迟后返回延迟时间
     *
     * @param delay 延迟处理(ms)|1000
     * @return
     * @throws InterruptedException
     */
    @GetMapping("/delay/{delay}")
    public long delay(@PathVariable long delay) throws InterruptedException {
        Thread.sleep(delay);
        return delay;
    }
}
